<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Category
 *
 * @author dung
 */
class Category extends Eloquent {

    //put your code here
    use SoftDeletingTrait;

    protected $dates = ['deleted_at'];
    public $basicRules = array(
        'title' => 'required|min:5',
    );

    public function saveItem($arrParams, $option = null) {
        $row = !empty($arrParams['id']) ? $this->find($arrParams['id']) : new Category();

        foreach ($arrParams as $key => $value) {
            if ($key != 'id')
                $row->{$key} = $value;
        }
        $row->save();
        return $row->id;
    }

    /*
     * 
     * @return list of lyrics
     */

    public function lyrics() {
        return $this->hasMany('Lyric');
    }

    public function lessons() {
        return $this->hasMany('Lesson');
    }

    public function videos() {
        return $this->hasMany('Video');
    }

    public function examVideos() {
        return $this->hasMany('Video', 'exam_level', 'id');
    }

    /**
     * @return mixed
     */
    public function Childs() {
        return $this->hasMany('Category', 'parent_id', 'id');
    }

    /**
     * @return mixed
     */
    public function Parent()
    {
    return $this->belongsTo('Category', 'parent_id', 'id');









    }

/*
 *
 */

public function getRootUnits() {
    return $this->where('parent_id', '=', 0)->where('type', '=', 'program')->get();
}

/**
 *
 */
public function dictionaries() {
    return $this->hasMany('Dictionary');
}

/**
 *
 */
public function lessonsInCourse() {
    return $this->belongsToMany('Lesson', 'categories_lessons', 'category_id', 'lesson_id')->withPivot('position', 'is_public');
}

/**
 *
 */
public function userCourse() {
    return $this->belongsToMany('User', 'users_categories', 'category_id', 'user_id')->withPivot('level', 'video_id', 'status');
}

public function questions() {
    return $this->hasMany('Question', 'category_id', 'id');
}

/*
 *
 */

public function levels() {
    return $this->hasMany('Category', 'parent_id', 'id');
}

/**
 *
 */
public function getRootUnitsToArray() {
    $units = $this->getRootUnits();
    $array = array();
    $array[0] = "Giáo trình chính";
    foreach ($units as $unit) {
        $array[$unit->id] = $unit->title;
    }
    return $array;
}

/* * *
 * @param string $type
 * @return array
 */

public static function ReturnToArray($type = 'tone') {
    $categories = Category::where('type', '=', $type)->get();
    $array = array();
    foreach ($categories as $category)
        $array[$category->id] = $category->title;
    return $array;
}

public function returnTypeToArray($arrParams) {
    $rs = $this->where('type', '=', $arrParams['type'])->get();
    $array = array();
    foreach ($rs as $row) {
        $array[$row->old] = $row->id;
    }
    return $array;
}

// huy list id cha con category
//public function dequy($result) {
//    if ($this->Childs()->count() > 0) {
//        array_push($result, $this->id);
//        foreach ($this->Childs()->select('id')->get() as $child) {
//            $result = $child->dequy($result);
//        }
//    } else {
//        array_push($result, $this->id);
//    }
//    return $result;
//}
public function dequy($array1, $array2) {
    $childs = $this->whereIn('parent_id', $array1)->select('id')->get();
    if (count($childs) > 0) {
        $array2 = array_unique(array_merge($array2, $array1));
        
        $array1 = array();
        foreach ($childs as $child) {
            array_push($array1, $child->id);
        }
        $array2 = $this->dequy($array1, $array2);
    } else {
        $array2 = array_unique(array_merge($array2, $array1));
    }
    return $array2;
}

}
